package cn.org.xuanzp.examples.v1;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * 
 * @author hy
 * @createTime 2021-04-17 10:36:18
 * @description 里主要用于传输文件的操作
 *
 */
public class NettyFtpServerHandler extends ChannelInboundHandlerAdapter {

	// 连接上后应该直接传输一个
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		// super.channelActive(ctx);
		System.out.println("FtpCommandHandler...............channelActive");
	}

	// 开始读取的操作
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			Command command = (Command) msg;
			CommandType type = command.getType();
			System.out.println("command type:" + type);
			Object obj = command;
			ctx.channel().writeAndFlush(obj);// 这里必须传递Object否则客户端出现不接收的问题
		} finally {
			ReferenceCountUtil.release(msg);
		}

	}

	// 出现异常的情况
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println(cause);
	}

}